using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Configuration;

namespace ProcessesSMS
{
	/// <summary>
	/// Summary description for.
	/// Project name: SMS_xoso
	/// channel: n/a
	/// command code: HN X Y
	/// created: 01/09/09 .
	/// Writer : Nampt + Tuanbc
	/// Company: VIT TELECOM
	/// </summary>

	public class XOSO_MT
	{
		#region Declare
		private SMS_MO WMO;
		private SMS_MT WMT;
		ArrayList arr = new ArrayList();

		public XOSO_MT(SMS_MO MO)
		{
			WMO = MO;
		}

		public XOSO_MT()
		{
		}
		#endregion
		#region Method
		public void run()
		{			
			WMT = new SMS_MT(WMO);
			// SMS_STR WSTR = new SMS_STR();
			string tmpMsg = WMO.Message.Substring(WMO.CommandCode.Length).Trim().Replace("  ", " ").Replace("  ", " ");
			tmpMsg = tmpMsg.Replace("  ", " ").Trim().ToUpper();
			string[] msg = tmpMsg.Split(new char[] { ' ' });
			string strReturnMsg = "";
            if (WMO.ServiceID.Replace("04", "") == "7062")
            {
                strReturnMsg = "Nhan ket qua XS theo tinh soan: XS MaTinh gui 7162.VD de nhan ket qua XS Khanh Hoa soan XS KH gui 7162";
            }
            else
            {
                strReturnMsg = GetReturnMsg(msg);                
            }
            WMT.AddMessage(WMO.UserID, strReturnMsg, SMS_ContentType.SMS_TEXT);
            WMT.MsgType = SMS_MsgType.SMS_TINHTIEN;
            WMT.Insert();
			return;

		}        
		private bool ValidInput(string[] tmp)
		{
			if (tmp.Length <= 0 || tmp.Length > 2) return false;
			return true;
		}
		private bool checkIsNumber(string txNumber)
		{
			bool textIsNumeric = true;
			try
			{
				int.Parse(txNumber.Trim());
			}
			catch
			{
				textIsNumeric = false;
			}
			return textIsNumeric;
		}	
		private string GetReturnMsg(string[] arr)
		{
            XOSO_WAIT xs_wait = new XOSO_WAIT();
			#region Khai báo kết nối
			string connString = ConfigurationSettings.AppSettings["CONNECTION_STRING_7x62"].ToString();
			SqlConnection oledbConn = new SqlConnection(connString);
			string strSQl = string.Empty;
			//SqlCommand cmd = new SqlCommand();
			SqlDataAdapter oleda;// = new SqlDataAdapter();
			DataSet ds;// = new DataSet();
			DataTable dt = new DataTable();
			string code = string.Empty;
			string msgBody = string.Empty;           
			oledbConn.Open();
			#endregion			
            //Neu nhan ket qua xo so moi nhat theo ma tinh
            if (arr.Length <= 1)
            {
                if (arr.Length == 0)
                {
                    strSQl = "SELECT TOP 1 * FROM XOSO WHERE mamien='MB' ORDER BY Ngay DESC";
                }
                else if ((arr[0] == "TD") || (arr[0] == "MB"))
                {
                    strSQl = "SELECT TOP 1 * FROM XOSO WHERE mamien='MB' ORDER BY Ngay DESC";
                }
                else
                {
                    strSQl = "SELECT TOP 1 * FROM XOSO WHERE Matinh='" + arr[0] + "' ORDER BY Ngay DESC";
                }
                oleda = new SqlDataAdapter(strSQl, oledbConn);
                ds = new DataSet();
                oleda.Fill(ds, "VIT");
                dt = ds.Tables["VIT"];                
                if (ds.Tables[0].Rows.Count > 0)
                {
                    DateTime dateTime = DateTime.Parse(dt.Rows[0]["Ngay"].ToString());
                    msgBody = string.Concat(arr[0].ToUpper() + ":", string.Concat(dateTime.Day, "/", dateTime.Month), "\n",
                        "DB:", dt.Rows[0]["GiaiDacBiet"].ToString().Trim().Replace(" ", ""), "\n",
                        "1:", dt.Rows[0]["GiaiNhat"].ToString().Trim().Replace(" ", ""), "\n",
                        "2:", dt.Rows[0]["GiaiNhi"].ToString().Trim().Replace(" ", ""), "\n",
                        "3:", dt.Rows[0]["GiaiBa"].ToString().Trim().Replace(" ", ""), "\n",
                        "4:", dt.Rows[0]["GiaiTu"].ToString().Trim().Replace(" ", ""), "\n",
                        "5:", dt.Rows[0]["GiaiNam"].ToString().Trim().Replace(" ", ""), "\n",
                        "6:", dt.Rows[0]["GiaiSau"].ToString().Trim().Replace(" ", ""), "\n",
                        "7:", dt.Rows[0]["GiaiBay"].ToString().Trim().Replace(" ", ""), "\n",
                        "8:", dt.Rows[0]["GiaiTam"].ToString().Trim().Replace(" ", ""));
                }
                else
                {
                    msgBody = "Nhan ks xs theo tinh: XS MaTinh gui 7162.Xem kq theo ngay: XS MaTinh Ngay/Thang gui 7162.VD: XS DN 15/09 gui 7162";
                }
            }
            //Nhan ket qua so xo theo ma tinh va theo ngay VD: XS DLK 2508 XS CT.01.09
            else
            {
                if (arr[1].ToUpper() == "CHO")
                {
                    msgBody = "Chung toi xac nhan thong tin ban chuyen toi tong dai 7x62.Chung toi se gui ket qua som nhat den cho ban.Cam on ban da su dung dich vu";
                    xs_wait.InsetSMS_WAIT(WMO.MO_ID, WMO.UserID, WMO.ServiceID, WMO.CommandCode, arr[0].Trim(), WMO.RequestID, WMO.RequestTime, 0, 1);
                }
                else
                {
                    //if ((checkIsNumber(arr[1])) && (arr[1].Trim().Length == 4))
                    //{
                    #region Trả về kết quả 1 tỉnh theo ngày
                    //string d = arr[1].Substring(0, 2) + "/" + arr[1].Substring(2, 2) + "/" + DateTime.Now.Year.ToString();
                    string d = arr[1] + "/" + DateTime.Now.Year.ToString();
                    if (arr[0] == "TD")
                    {
                        strSQl = "SELECT TOP 1 * FROM XOSO WHERE mamien='MB' And Convert(nvarchar(10),ngay,103)='" + d + "'";
                    }
                    else
                    {
                        strSQl = "SELECT TOP 1 * FROM XOSO WHERE Matinh='" + arr[0] + "' And Convert(nvarchar(10),ngay,103)='" + d + "'";
                    }
                    oleda = new SqlDataAdapter(strSQl, oledbConn);
                    ds = new DataSet();
                    oleda.Fill(ds, "VIT");
                    dt = ds.Tables["VIT"];
                    if (dt.Rows.Count > 0)
                    {
                        msgBody = string.Concat(arr[0].ToUpper() + ": ", d, "\n",
                        "DB:", dt.Rows[0]["GiaiDacBiet"].ToString().Trim().Replace(" ", ""), "\n",
                        "1:", dt.Rows[0]["GiaiNhat"].ToString().Trim().Replace(" ", ""), "\n",
                        "2:", dt.Rows[0]["GiaiNhi"].ToString().Trim().Replace(" ", ""), "\n",
                        "3:", dt.Rows[0]["GiaiBa"].ToString().Trim().Replace(" ", ""), "\n",
                        "4:", dt.Rows[0]["GiaiTu"].ToString().Trim().Replace(" ", ""), "\n",
                        "5:", dt.Rows[0]["GiaiNam"].ToString().Trim().Replace(" ", ""), "\n",
                        "6:", dt.Rows[0]["GiaiSau"].ToString().Trim().Replace(" ", ""), "\n",
                        "7:", dt.Rows[0]["GiaiBay"].ToString().Trim().Replace(" ", ""), "\n",
                        "8:", dt.Rows[0]["GiaiTam"].ToString().Trim().Replace(" ", ""));
                    }
                    else
                    {
                        msgBody = "MaTinh la cac chu cai dau ten tinh.Cac tinh ngoai le:DaNang: DNG,BinhDinh: BDH,DacLak: DLK,DacNong: DNO,QuangNam: DNM,QuangNgai: QNI,BenTre: BTR,BinhThuan: BTH";
                    }
                    #endregion
                    //}
                    //else//nhan sai ngay thang
                    //{
                    //    msgBody = "Nhan kq xs mot tinh theo ngay soan tin: XS MaTinh NgayThang gui 7162.VD:Nhan kq xs tinh Khanh Hoa ngay 15 thang 9 soan tin: XS KH 1509 gui 7162";
                    //}
                }
            }           
			oledbConn.Close();
            oledbConn.Dispose();
			return msgBody;
		}
		#endregion Method
	}
}
